<?php

/**
 * @author Tuan Anh
 * @copyright 2012
 */
class loginController extends baseController{
    public function index(){
        
        if(isset($_COOKIE['username']) && $_COOKIE['hash']){ //if site has cookie

            $username = $_COOKIE['username'];
            $hash = $_COOKIE['hash'];
            
            if(($uid = $this->registry->db->getManager("userManager")->login($username, $hash))!="-1"){
                $_SESSION['username']   = $username;
                $_SESSION['uid']        = $uid;

            }
            header("Location:".__SERVER);
        }
        else
        {                                        //if site has not cookie
            if(isset($_POST['submit'])){
                $username = $_POST['username'];
                $hash = $this->registry->db->getManager("userManager")->hashPassword($_POST['password']);
                
                if(($uid = $this->registry->db->getManager("userManager")->login(mysql_real_escape_string($username), $hash))!="-1"){
                    if($_POST['remmemberme'] == 1){
                        setcookie("username", $username, time()+60*60*24*30,"/");
                        setcookie("hash", $hash, time()+60*60*24*365, "/");
                    }
                    
                    $_SESSION['username']   = $username;
                    $_SESSION['uid']        = $uid;
                header("Location:".__SERVER);    
                }else{
                    //when wrong username, password
                    $this->master($this->registry->template->masterbody("Sai thông tin"));
                }
                
                
            }
            else{                                                          //if site has no $_POST

                $tags = array();
                echo $this->registry->template->getTemplate("login", $tags);
                
            }
        }


    }
    
    public function register(){ 
        if(isset($_POST['submit'])){      //if site has post
            if($_POST['username'] == "" or $_POST['password'] == "" or $_POST['repassword'] == "" or $_POST['email'] == ""){
                //TO_DO
                $this->master($this->registry->template->masterbody("Vui lòng nhập tất cả thông tin!", array()));
            }
            else if($_POST['password'] != $_POST['repassword']){
                //TO_DO
                $this->master($this->registry->template->masterbody("Mật khẩu không trùng khớp!!", array()));
            }
            else if($this->registry->db->getManager("userManager")->checkUsername($_POST['username'])){
                $this->master($this->registry->template->masterbody("Tên đăng nhập đã được sử dụng. Vui lòng chọn tên đăng nhập khác!!", array()));
            }
            else{
                $hash = $this->registry->db->getManager("userManager")->hashPassword($_POST['password']);
                $this->registry->db->getManager("userManager")->register($_POST['username'], $hash, $_POST['email'] );
                $this->master($this->registry->template->masterbody("Đăng kí thành công!", array()));

            }
            header("Location:".__SERVER);
        }else{                              //if site has no post
            echo $this->registry->template->getTemplate("register", array());
        }
    }
    
    public function forgot(){
        //TO-DO
        if(isset($_POST['submit'])){
            if($_POST['email'] == ''){
                $this->master($this->registry->template->masterbody("Vui lòng nhập đủ thông tin!"));
            }else{
                require_once('mail/phpmailer.class.php');
                require_once('mail/smtp.class.php');

                $mail             = new PHPMailer(); // defaults to using php "mail()"

                $body             = "Mật khẩu mới của bạn là 111111";

                $mail->IsSMTP(); // telling the class to use SMTP
                $mail->Host       = "mail.gmail.com"; // SMTP server
                $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                // 1 = errors and messages
                // 2 = messages only
                $mail->SMTPAuth   = true;                  // enable SMTP authentication
                $mail->SMTPSecure = "tls";                 // sets the prefix to the servier
                $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
                $mail->Port       = 587;                   // set the SMTP port for the GMAIL server
                $mail->Username   = "photro33@gmail.com";  // GMAIL username
                $mail->Password   = "superwinner";            // GMAIL password

                $mail->AddReplyTo("anh_nguyen9123@yahoo.com","photro.vn");

                $mail->SetFrom('anh_nguyen9123@yahoo.com', 'photro.vn');

                $mail->AddReplyTo("anh_nguyen9123@yahoo.com","photro.vn");

                $address = "anh_nguyen9123@yahoo.com";
                $mail->AddAddress($address, "Tuấn Anh");

                $mail->Subject    = "PHPMailer Test Subject via mail(), basic";

                $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

                $mail->MsgHTML($body);


                if(!$mail->Send()) {
                    echo "Mailer Error: " . $mail->ErrorInfo;
                } else {
                    echo "Message sent!";
                }

            }
        }
    }
}


?>